package com.cn.utilTwo;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class YzmTest {
    public void Test(HttpServletRequest request) throws IOException {
        //创建工具类对象
        YzmUtils yzmUtils = new YzmUtils();
        //调用createImage方法
        Object[] objs = yzmUtils.createImage();
        //获取工具类中生成的验证码图片
        BufferedImage image = (BufferedImage) objs[1];
        //获取工具类中生成的验证码字符串
        request.getSession().setAttribute("code", objs[0]);
        //在项目下生成验证码文件
        //注：webRoot目录下要有yzm这个文件夹
        OutputStream os = new FileOutputStream(request.getSession().getServletContext().getRealPath("") + "/yzm/" + System.currentTimeMillis() + ".jpg");
        //生成验证码图片的路径，在jsp页面需要拼接项目名
        String path = "/yzm/" + System.currentTimeMillis() + ".jpg";
        ImageIO.write(image, "jpg", os);
        os.close();
    }


}
